<template>
  <div class="preview-container" v-if="visible">
    <div class="preview-overlay" @click="closePreview"></div>
    <div class="preview-content">
      <div class="preview-header">
        <span class="file-name">{{ fileInfo?.name || '' }}</span>
        <span class="close-btn" @click="closePreview">×</span>
      </div>
      
      <!-- 图片预览 -->
      <div v-if="fileType === 'image'" class="image-preview">
        <img :src="previewUrl" :alt="fileInfo?.name || ''" class="preview-image" />
      </div>
      
      <!-- 视频预览 -->
      <div v-else-if="fileType === 'video'" class="video-preview">
        <video :src="previewUrl" :poster="fileInfo?.poster || ''" controls class="preview-video">
          您的浏览器不支持HTML5视频播放
        </video>
      </div>
      
      <!-- 文件详情 -->
      <div class="file-details" v-if="fileInfo">
        <div class="detail-item">
          <span class="label">文件名：</span>
          <span class="value">{{ fileInfo.name }}</span>
        </div>
        <div class="detail-item">
          <span class="label">文件类型：</span>
          <span class="value">{{ fileInfo.type || '未知' }}</span>
        </div>
        <div class="detail-item">
          <span class="label">文件大小：</span>
          <span class="value">{{ formatFileSize(fileInfo.size) }}</span>
        </div>
        <div class="detail-item">
          <span class="label">创建时间：</span>
          <span class="value">{{ formatDate(fileInfo.createTime) }}</span>
        </div>
        <div class="detail-item" v-if="fileInfo.updateTime">
          <span class="label">修改时间：</span>
          <span class="value">{{ formatDate(fileInfo.updateTime) }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue'
import { ElMessage } from 'element-plus'
import { formatFileSize, isImageFile, isVideoFile } from '../utils/fileUtils.js'
import { FileType } from '../utils/mockData.js'

// Props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  fileInfo: {
    type: Object,
    default: () => ({})
  }
})

// Emits
const emit = defineEmits(['update:visible'])

// 文件类型判断
const fileType = computed(() => {
  if (!props.fileInfo || !props.fileInfo.filetype) return 'other'
  
  try {
    if (isImageFile(props.fileInfo.filetype)) {
      return 'image'
    } else if (isVideoFile(props.fileInfo.filetype)) {
      return 'video'
    }
    
    // 适配FileType枚举
    const fileTypeNum = parseInt(props.fileInfo.filetype)
    switch (fileTypeNum) {
      case FileType.FOLDER:
      case FileType.HIDE_DIRECTORY:
        return 'folder'
      case FileType.IMAGE:
        return 'image'
      case FileType.DOCUMENT:
        return 'document'
      case FileType.AUDIO:
        return 'audio'
      case FileType.VIDEO:
        return 'video'
      default:
        return 'other'
    }
  } catch (e) {
    // 回退到原始判断逻辑
    if (isImageFile(props.fileInfo.filetype)) {
      return 'image'
    } else if (isVideoFile(props.fileInfo.filetype)) {
      return 'video'
    }
    return 'other'
  }
})

// 计算属性：文件大小显示
const fileSizeDisplay = computed(() => {
  return formatFileSize(parseInt(props.fileInfo?.fsize || props.fileInfo?.size || '0'))
})

// 监听可见性变化
watch(() => props.visible, (newVal) => {
  if (newVal) {
    // 禁止背景滚动
    document.body.style.overflow = 'hidden'
    loadFileData()
  } else {
    // 恢复背景滚动
    document.body.style.overflow = 'auto'
  }
})

// 加载文件数据
const isLoading = ref(false)
const loadFileData = () => {
  isLoading.value = true
  // 在实际项目中，这里应该是一个API调用
  
  // 模拟数据加载延迟
  setTimeout(() => {
    isLoading.value = false
    
    // 根据文件类型设置预览数据
    if (isImageFile(props.fileInfo?.filetype)) {
      // 图片类型可能需要额外处理
    } else if (isVideoFile(props.fileInfo?.filetype)) {
      // 视频类型可能需要额外处理
    }
  }, 300)
}

// 方法
const closePreview = () => {
  emit('update:visible', false)
}

// 计算预览URL
const previewUrl = computed(() => {
  const url = props.fileInfo?.url
  const fid = props.fileInfo?.fid
  
  // 如果有直接的url则使用，否则构建一个基础URL
  if (url) return url
  if (fid) return `/api/v1/oss/view?fid=${fid}`
  return ''
})

// 使用导入的formatFileSize函数

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
</script>

<style scoped>
.preview-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
}

.preview-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
}

.preview-content {
  position: relative;
  max-width: 90%;
  max-height: 90vh;
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
}

.file-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.close-btn {
  font-size: 24px;
  color: #666;
  cursor: pointer;
  padding: 0;
  margin: 0;
  background: none;
  border: none;
  width: 30px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 4px;
  transition: all 0.3s;
}

.close-btn:hover {
  background-color: #e0e0e0;
  color: #333;
}

.image-preview,
.video-preview {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background-color: #fafafa;
}

.preview-image {
  max-width: 100%;
  max-height: 60vh;
  object-fit: contain;
  border-radius: 4px;
}

.preview-video {
  max-width: 100%;
  max-height: 60vh;
  border-radius: 4px;
}

.file-details {
  padding: 20px;
  background-color: #f9f9f9;
  border-top: 1px solid #e0e0e0;
}

.detail-item {
  display: flex;
  margin-bottom: 10px;
  font-size: 14px;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.detail-item .label {
  width: 100px;
  color: #666;
  font-weight: 500;
}

.detail-item .value {
  color: #333;
  flex: 1;
  word-break: break-all;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .preview-content {
    max-width: 95%;
    max-height: 95vh;
  }
  
  .preview-header {
    padding: 10px 15px;
  }
  
  .file-name {
    font-size: 14px;
    max-width: 60%;
  }
  
  .image-preview,
  .video-preview {
    padding: 10px;
  }
  
  .file-details {
    padding: 15px;
  }
  
  .detail-item {
    font-size: 13px;
  }
  
  .detail-item .label {
    width: 80px;
  }
}
</style>